package com.sgb.kjwl.viewmodel.shopping;


import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ShopLeaseEntity;
import com.sgb.kjwl.model.entity.TypeEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.ShoppingCarRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.shop.ShopMachineChildFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class ShopMachinekChildModel extends BaseViewModel {
    private ShopMachineChildFragment fragment;
    private ShoppingCarRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public ShopMachinekChildModel(ShopMachineChildFragment fragment, ShoppingCarRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    /**
     * @param websiteNo   企业no或个人userNo
     * @param type        租赁类型
     * @param websiteType 网站类型(0为个人,1为企业)
     * @param isLoadMore
     */
    public void queryListByStore(String websiteNo, String websiteType, String type, boolean isLoadMore) {
        page = isLoadMore ? ++page : 1;
        HashMap map = new HashMap<>();
        map.put("type", type);//租赁类型
        if ("0".equals(websiteType)) {
            map.put("userNo", websiteNo);
        } else {
            map.put("companyNo", websiteNo);//企业no
        }
        map.put("pageNum", page);
        map.put("pageSize", size);
        APIManager.getInstance().getPickAPI().getLeaseImg(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<ShopLeaseEntity>>>>(true) {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<ShopLeaseEntity>>> data) {
                if (isLoadMore) {
                    fragment.loadMoreList(data.getData().getList());
                } else {
                    fragment.refreshList(data.getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ShopLeaseEntity>>>> call, Throwable t) {
                fragment.requestComplete();
            }
        });
    }

    /**
     * @param websiteNo   企业no或个人userNo
     * @param websiteType 网站类型
     */
    public void groupBuyClassList(String websiteNo, String websiteType) {
        HashMap map = new HashMap<>();
        map.put("module", "0"); //模块，0：出租，1：求租
        map.put("createType", "0".equals(websiteType) ? "2" : "1");//创建类型，1 企业  2个人
        if ("0".equals(websiteType)) {
            map.put("userNo", websiteNo);//用户no
        } else {
            map.put("companyNo", websiteNo);//企业no
        }
        APIManager.getInstance().getPickAPI().getLeaseType(map).enqueue(new ICallBack<BaseEntity<List<TypeEntity>>>(true) {
            @Override
            public void onSuccess(BaseEntity<List<TypeEntity>> data) {
                if (data != null) {
                    fragment.updateViews(data.getData());
                } else {
                    fragment.empty();
                }
            }
        });
    }

    //获取详情H5
    public void getH5Url(int type, String id) {
        String name = "";
        if (type == 1) {
            name = "mechanicsDetail";//机械
        } else if (type == 2) {
            name = "equipmentDetails";//设备
        } else if (type == 3) {
            name = "vehicleDetails";//车辆
        } else if (type == 4) {
            name = "propertyDetails";//房产
        } else if (type == 5) {
            name = "landDetails";//土地
        } else if (type == 6) {
            name = "factoryDetails";//工厂
        } else if (type == 7) {
            name = "farmDetail";//农机
        }
        APIManager.getInstance().getPickAPI().getUrlSetting(name).enqueue(new ICallBack<BaseEntity<H5ReEntity>>(true) {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data != null) {
                    String url = data.getData().getUrl() + "?id=" + id + "&iosFlag=1";
                    WebViewX5HostActivity.start(fragment.getActivity(), url);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

}
